package org.ioriframework.plugin.maven.console;

import java.io.IOException;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsolePageParticipant;
import org.eclipse.ui.part.IPageBookViewPage;
import org.eclipse.ui.part.IPageSite;
import org.ioriframework.plugin.maven.Constants;
import org.ioriframework.plugin.maven.util.ProcessUtils;

public class ConsoleActions implements IConsolePageParticipant {
	
	private IPageBookViewPage page;
	
	private Action terminate;
	
	private Action remove;
	
	private Action removeAll;
	
	private IActionBars bars;
	
	private Console console;

	@Override
	public <T> T getAdapter(Class<T> arg0) {
		return null;
	}

	@Override
	public void activated() {
		updateBtn();
	}

	@Override
	public void deactivated() {
		updateBtn();
	}

	@Override
	public void dispose() {
		this.removeAll = null;
		this.remove = null;
		this.terminate = null;
		this.bars = null;
		this.page = null;
	}

	@Override
	public void init(IPageBookViewPage page, IConsole console) {
		this.console = (Console) console;
		this.page = page;
		IPageSite site = page.getSite();
		this.bars = site.getActionBars();
		
		createTerminateButton();
		createRemoveButton();
		createRemoveAllButton();
		
//		bars.getMenuManager().add(new Separator());
//		bars.getMenuManager().add(remove);
		
		IToolBarManager toolBarManager = bars.getToolBarManager();
		toolBarManager.appendToGroup(IConsoleConstants.LAUNCH_GROUP, terminate);
		toolBarManager.appendToGroup(IConsoleConstants.LAUNCH_GROUP, remove);
		toolBarManager.appendToGroup(IConsoleConstants.LAUNCH_GROUP, removeAll);
		bars.updateActionBars();
	}
	
	private void createTerminateButton() {
		// 按钮图标
		ImageDescriptor imageDescriptor = ImageDescriptor.createFromFile(getClass(), "/icons/terminate.gif");
		ImageDescriptor disabledImageDescriptor = ImageDescriptor.createFromFile(getClass(), "/icons/terminate_disable.gif");
		this.terminate = new Action(Constants.ACTION_TEXT_TERMINATE, imageDescriptor) {
			public void run() {
				if (console != null) {
					// 标识为强制终止任务
					console.setForceTerminate(true);
					try {
						ProcessUtils.killByPid(console.getPid());
					} catch (IOException e) {
						// 命令执行异常不处理
					}
				}
			}
		};
		this.terminate.setDisabledImageDescriptor(disabledImageDescriptor);
	}
	
	private void createRemoveButton() {
		// 按钮图标
		ImageDescriptor imageDescriptor = ImageDescriptor.createFromFile(getClass(), "/icons/remove.gif");
		ImageDescriptor disabledImageDescriptor = ImageDescriptor.createFromFile(getClass(), "/icons/remove_disable.gif");
		this.remove = new Action(Constants.ACTION_TEXT_REMOVE, imageDescriptor) {
			public void run() {
				// 关闭当前console
				IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
				if (console != null && !console.isRunning()) {
					manager.removeConsoles(new IConsole[] {console});
				}
			}
		};
		this.remove.setDisabledImageDescriptor(disabledImageDescriptor);
		// 关闭窗口按钮初始是禁用的，当窗口的任务执行完成后启用
		this.remove.setEnabled(false);
	}
	
	private void createRemoveAllButton() {
		// 按钮图标
		ImageDescriptor imageDescriptor = ImageDescriptor.createFromFile(getClass(), "/icons/removeAll.gif");
		ImageDescriptor disabledImageDescriptor = ImageDescriptor.createFromFile(getClass(), "/icons/removeAll_disable.gif");
		this.removeAll = new Action(Constants.ACTION_TEXT_REMOVEALL, imageDescriptor) {
			public void run() {
				// 关闭所有任务已经执行完毕的console
				IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
				// 所有的console
				IConsole[] consoles = manager.getConsoles();
				for (IConsole console : consoles) {
					// 如果console是JettyConsole, 并且任务已经执行完毕，则关闭窗口
					if (console instanceof Console) {
						Console jettyConsole = (Console) console;
						if (!jettyConsole.isRunning()) {
							manager.removeConsoles(new IConsole[] {jettyConsole});
						}
					}
				}
			}
		};
		this.removeAll.setDisabledImageDescriptor(disabledImageDescriptor);
		// 关闭窗口按钮初始是禁用的，当窗口的任务执行完成后启用
		this.removeAll.setEnabled(false);
	}
	
	/**
	 * 刷新按钮状态
	 */
	private void updateBtn() {
		if (this.page == null) {
			return;
		}
		boolean isRunning = this.console.isRunning();
		this.terminate.setEnabled(isRunning);
		this.remove.setEnabled(!isRunning);
		this.removeAll.setEnabled(!isRunning);
		this.bars.updateActionBars();
	}

}
